﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cotrobaie.WebServicies;
using Cotrobaie.UsersWebService;

namespace Cotrobaie
{
    public partial class Search : System.Web.UI.Page
    {
        public static int produsePagina = 1;
        protected override void  OnInit(EventArgs e)
        {
            if (!Page.IsPostBack)
                setPlaceHolder1(Request.Params["KeyWord"], Request.Params["ProductsID"], Request.Params["categoryID"], Request.Params["PriceMin"], Request.Params["PriceMax"],
                    Request.Params["Pg"], Request.Params["IntrariPg"], Request.Params["ordonareTitlu"], Request.Params["ordonarePret"], Request.Params["ordonareAutor"],
                    Request.Params["ordonareBrand"], Request.Params["ordonareCategorie"]);
            base.OnInit(e);
        }
        private void setPlaceHolder1(string KeyWord, string ProductsID, string categoryID, string PriceMin, string PriceMax, string Pg, string IntrariPg, 
            string ordonareTitlu, string ordonarePret, string ordonareAutor, string ordonareBrand, string ordonareCategorie)
        {
            PlaceHolder1.Attributes["src"] = "UserControl/Default.aspx?" +
                    (Server.UrlDecode(KeyWord) != null ? "KeyWord=" + KeyWord + "&" : String.Empty) +
                    (Server.UrlDecode(ProductsID) != null ? "ProductsID=" + ProductsID + "&" : String.Empty) +
                    (Server.UrlDecode(categoryID) != null ? "categoryID=" + categoryID + "&" : String.Empty) +
                    (Server.UrlDecode(PriceMin) != null ? "PriceMin=" + PriceMin + "&" : String.Empty) +
                    (Server.UrlDecode(PriceMax) != null ? "PriceMax=" + PriceMax + "&" : String.Empty) +
                    (Server.UrlDecode(Pg) != null ? "Pg=" + Pg + "&" : String.Empty) +
                    (Server.UrlDecode(IntrariPg) != null ? "IntrariPg=" + IntrariPg + "&" : String.Empty) +
                    (Server.UrlDecode(ordonareTitlu) != null ? "ordonareTitlu=" + ordonareTitlu + "&" : String.Empty) +
                    (Server.UrlDecode(ordonarePret) != null ? "ordonarePret=" + ordonarePret + "&" : String.Empty) +
                    (Server.UrlDecode(ordonareAutor) != null ? "ordonareAutor=" + ordonareAutor + "&" : String.Empty) +
                    (Server.UrlDecode(ordonareBrand) != null ? "ordonareBrand=" + ordonareBrand + "&" : String.Empty) +
                    (Server.UrlDecode(ordonareCategorie) != null ? "ordonareCategorie=" + ordonareCategorie + "&" : String.Empty);
                    
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) Session["ProductsCurrPage"] = null;
            if (!Page.IsPostBack) UmpleCategorii();
            LabelCurrentPage.Text = Session["ProductsCurrPage"] != null ? (((int)Session["ProductsCurrPage"] + 1).ToString()) : (Request.Params["Pg"] != null ? (int.Parse(Server.UrlDecode(Request.Params["Pg"])) + 1).ToString() : "1");
            LabelCurrentPage1.Text = Session["ProductsCurrPage"] != null ? (((int)Session["ProductsCurrPage"] + 1).ToString()) : (Request.Params["Pg"] != null ? (int.Parse(Server.UrlDecode(Request.Params["Pg"])) + 1).ToString() : "1");
            if (Session["ProductsCurrPage"] == null)
                Session["ProductsCurrPage"] = 0;
            if ((int)Session["ProductsCurrPage"] == 0)
            {
                ButtonPrevPage.Enabled = false; ButtonPrevPage.CssClass = "button3";
                ButtonPrevPage1.Enabled = false; ButtonPrevPage1.CssClass = "button3";
            }
            Panel pp = (Panel)this.Master.FindControl("Panel1");
            pp.Style.Add("position", "relative");
            pp.Style.Add("top","170px");

           
        }

        private void UmpleCategorii()
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                var result = serviciu.GetCategorii();
                if (result.Success)
                {
                    DropDownListCategory.Items.Clear();
                    DropDownListCategory.Items.Add(new System.Web.UI.WebControls.ListItem("All", (-1).ToString()));
                    DropDownListCategory.Items.Add(new System.Web.UI.WebControls.ListItem(ToTitleCase(result.Categorii[0].name), result.Categorii[0].ID.ToString()));
                    foreach (var v in result.Categorii[0].SubCategorii)
                        DropDownListCategory.Items.Add(new System.Web.UI.WebControls.ListItem(ToTitleCase(v.name), v.ID.ToString()));
                    DropDownListCategory.Items.Add(new System.Web.UI.WebControls.ListItem(ToTitleCase(result.Categorii[1].name), result.Categorii[1].ID.ToString()));
                    foreach (var v in result.Categorii[1].SubCategorii)
                        DropDownListCategory.Items.Add(new System.Web.UI.WebControls.ListItem(ToTitleCase(v.name), v.ID.ToString()));
                    DropDownListCategory.Items.Add(new System.Web.UI.WebControls.ListItem(ToTitleCase(result.Categorii[2].name), result.Categorii[2].ID.ToString()));
                    foreach (var v in result.Categorii[2].SubCategorii)
                        DropDownListCategory.Items.Add(new System.Web.UI.WebControls.ListItem(ToTitleCase(v.name), v.ID.ToString()));
                }
            }
        }
        private string ToTitleCase(string p)
        { return p.Substring(0, 1).ToUpper() + p.Substring(1); }
        
        protected void ButtonPrevPage_Click(object sender, EventArgs e)
        {
            if (Session["ProductsCurrPage"] != null)
            {
                if ((int)Session["ProductsCurrPage"] > 0)
                    Session["ProductsCurrPage"] = int.Parse(Session["ProductsCurrPage"].ToString()) - 1;
                if ((int)Session["ProductsCurrPage"] == 0) 
                { 
                    ButtonPrevPage.Enabled = false; ButtonPrevPage.CssClass = "button3";
                    ButtonPrevPage1.Enabled = false; ButtonPrevPage1.CssClass = "button3";
                }
            }
            else
            {
                Session["ProductsCurrPage"] = Server.UrlDecode(Request.Params["Pg"]) != null ? Server.UrlDecode(Request.Params["Pg"]) : 0.ToString();
                if ((int)Session["ProductsCurrPage"] > 0) Session["ProductsCurrPage"] = int.Parse(Session["ProductsCurrPage"].ToString()) - 1;
                if ((int)Session["ProductsCurrPage"] == 0) 
                { 
                    ButtonPrevPage.Enabled = false; ButtonPrevPage.CssClass = "button3";
                    ButtonPrevPage1.Enabled = false; ButtonPrevPage1.CssClass = "button3";
                }
            }
            setPlaceHolder1(Request.Params["KeyWord"], Request.Params["ProductsID"], (DropDownListCategory.SelectedValue != "-1" ? DropDownListCategory.SelectedValue : (Request.Params["categoryID"] != null ? Request.Params["categoryID"] : "-1")),
                (TextBoxPriceMin.Text != String.Empty ? TextBoxPriceMin.Text : Request.Params["PriceMin"]), (TextBoxPriceMax.Text != String.Empty ? TextBoxPriceMax.Text : Request.Params["PriceMax"]),
                Session["ProductsCurrPage"].ToString(), Request.Params["IntrariPg"],
                (DropDownListOrderBy.SelectedValue == "Title" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareTitlu"]),
                (DropDownListOrderBy.SelectedValue == "Price" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonarePret"]),
                (DropDownListOrderBy.SelectedValue == "Author" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareAutor"]),
                (DropDownListOrderBy.SelectedValue == "Brand" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareBrand"]),
                (DropDownListOrderBy.SelectedValue == "Category" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareCategorie"]));
            ButtonNextPage.Enabled = true;
            ButtonNextPage.CssClass = "button2";
            ButtonNextPage1.Enabled = true;
            ButtonNextPage1.CssClass = "button2";
            Page_Load(null, null);
        }

        protected void ButtonNextPage_Click(object sender, EventArgs e)
        {
            if (Session["ProductsCurrPage"] != null)
                if (produsePagina > 0)
                    Session["ProductsCurrPage"] = int.Parse(Session["ProductsCurrPage"].ToString()) + 1;
                else 
                {
                    ButtonNextPage.Enabled = false; ButtonNextPage.CssClass = "button3";
                    ButtonNextPage1.Enabled = false; ButtonNextPage1.CssClass = "button3"; 
                }
            else
            {
                Session["ProductsCurrPage"] = Server.UrlDecode(Request.Params["Pg"]) != null ? Server.UrlDecode(Request.Params["Pg"]) : 0.ToString();
                if (produsePagina > 0) Session["ProductsCurrPage"] = int.Parse(Session["ProductsCurrPage"].ToString()) + 1;
                else 
                {
                    ButtonNextPage1.Enabled = false; ButtonNextPage1.CssClass = "button3"; 
                    ButtonNextPage.Enabled = false; ButtonNextPage.CssClass = "button3"; 
                }
            }
            setPlaceHolder1(Request.Params["KeyWord"], Request.Params["ProductsID"], (DropDownListCategory.SelectedValue != "-1" ? DropDownListCategory.SelectedValue : (Request.Params["categoryID"] != null ? Request.Params["categoryID"] : "-1")),
                (TextBoxPriceMin.Text != String.Empty ? TextBoxPriceMin.Text : Request.Params["PriceMin"]), (TextBoxPriceMax.Text != String.Empty ? TextBoxPriceMax.Text : Request.Params["PriceMax"]),
                Session["ProductsCurrPage"].ToString(), Request.Params["IntrariPg"],
                (DropDownListOrderBy.SelectedValue == "Title" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareTitlu"]),
                (DropDownListOrderBy.SelectedValue == "Price" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonarePret"]),
                (DropDownListOrderBy.SelectedValue == "Author" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareAutor"]),
                (DropDownListOrderBy.SelectedValue == "Brand" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareBrand"]),
                (DropDownListOrderBy.SelectedValue == "Category" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareCategorie"]));
            ButtonPrevPage.Enabled = true;
            ButtonPrevPage.CssClass = "button2";
            ButtonPrevPage1.Enabled = true;
            ButtonPrevPage1.CssClass = "button2";
            Page_Load(null, null);
        }

        public void changeIcon(object sender, EventArgs e)
        {
            Session["ProductsCurrPage"] = 0;
            setPlaceHolder1(Request.Params["KeyWord"], Request.Params["ProductsID"], (DropDownListCategory.SelectedValue != "-1" ? DropDownListCategory.SelectedValue : (Request.Params["categoryID"] != null ? Request.Params["categoryID"] : "-1")),
                (TextBoxPriceMin.Text != String.Empty ? TextBoxPriceMin.Text : Request.Params["PriceMin"]), (TextBoxPriceMax.Text != String.Empty ? TextBoxPriceMax.Text : Request.Params["PriceMax"]),
                (Session["ProductsCurrPage"] != null ? Session["ProductsCurrPage"].ToString() : Request.Params["Pg"]), Request.Params["IntrariPg"],
                (DropDownListOrderBy.SelectedValue == "Title" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareTitlu"]),
                (DropDownListOrderBy.SelectedValue == "Price" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonarePret"]),
                (DropDownListOrderBy.SelectedValue == "Author" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareAutor"]),
                (DropDownListOrderBy.SelectedValue == "Brand" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareBrand"]),
                (DropDownListOrderBy.SelectedValue == "Category" ? (DropDownListOrdering.SelectedValue == "Ascedent" ? "Crescator" : (DropDownListOrdering.SelectedValue == "Descendent" ? "Descreascator" : "Deloc")) : Request.Params["ordonareCategorie"]));
            Page_Load(null, null);
        }
        protected void goHome(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }
    }
}